import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  // {
  //   path: "/test-management",
  //   component: Layout,
  //   redirect: "/test",
  //   name: "TestManagement",
  //   children: [
  //     {
  //       path: "/test",
  //       component: () => import("@/views/test/index.vue"),
  //       name: "Test",
  //       meta: { title: "测试", icon: "business" },
  //     },
  //   ],
  // },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
    ],
  },
  {
    path: "/system-management",
    component: Layout,
    redirect: "/system-management",
    name: "SystemManagement",
    meta: { title: "系统管理", icon: "system" },
    children: [
      {
        path: "/administrator",
        component: () => import("@/views/administrator/index.vue"),
        name: "Administrator",
        meta: { title: "管理员", icon: "el-icon-User" },
      },
      {
        path: "/roles",
        component: () => import("@/views/roles/index.vue"),
        name: "Roles",
        meta: { title: "角色管理", icon: "role" },
      },
      {
        path: "/menu",
        component: () => import("@/views/menu/index.vue"),
        name: "Menu",
        meta: { title: "系统菜单", icon: "menu" },
      },
      {
        path: "/wx-program",
        component: () => import("@/views/wxprogram/index.vue"),
        name: "WxProgram",
        meta: { title: "微信小程序", icon: "el-icon-Cellphone" },
      },
    ],
  },
  {
    path: "/user-management",
    component: Layout,
    redirect: "/user-management",
    name: "UserManagement",
    meta: { title: "用户管理", icon: "roles" },
    children: [
      {
        path: "/customers",
        component: () => import("@/views/customers/index.vue"),
        name: "Customers",
        meta: { title: "客户", icon: "client" },
      },
      {
        path: "/lawyers",
        component: () => import("@/views/lawyers/index.vue"),
        name: "Lawyers",
        meta: { title: "律师", icon: "lawyer" },
      },
    ],
  },
  {
    path: "/business-management",
    component: Layout,
    redirect: "/business",
    name: "BusinessManagement",
    children: [
      {
        path: "/business",
        component: () => import("@/views/business/index.vue"),
        name: "Business",
        meta: { title: "律师业务", icon: "business" },
      },
    ],
  },
  {
    path: "/job-management",
    component: Layout,
    redirect: "/job",
    name: "JobManagement",
    children: [
      {
        path: "/job",
        component: () => import("@/views/job/index.vue"),
        name: "Job",
        meta: { title: "律师职位", icon: "job" },
      },
    ],
  },
  {
    path: "/cases-management",
    component: Layout,
    redirect: "/cases-management",
    name: "CasesManagement",
    children: [
      {
        path: "/cases",
        component: () => import("@/views/cases/index.vue"),
        name: "Cases",
        meta: { title: "案件", icon: "cases" },
      },
    ],
  },
  {
    path: "/instruments-management",
    component: Layout,
    redirect: "/instruments-management",
    name: "InstrumentsManagement",
    meta: { title: "文书管理", icon: "instruments-management" },
    children: [
      {
        path: "/instruments-category",
        component: () => import("@/views/inscategory/index.vue"),
        name: "InstrumentsCategory",
        meta: { title: "文书类别", icon: "instruments-category" },
      },
      {
        path: "/instruments-type",
        component: () => import("@/views/instype/index.vue"),
        name: "InstrumentsType",
        meta: { title: "文书分类", icon: "instruments-type" },
      },
      {
        path: "/instruments",
        component: () => import("@/views/instruments/index.vue"),
        name: "Instruments",
        meta: { title: "文书代写", icon: "instruments" },
      },
    ],
  },
  {
    path: "/lawfirm-management",
    component: Layout,
    redirect: "/lawfirm-management",
    name: "LawfirmManagement",
    children: [
      {
        path: "/lawfirm",
        component: () => import("@/views/lawfirm/index.vue"),
        name: "Lawfirm",
        meta: { title: "律所管理", icon: "lawfirm" },
      },
    ],
  },
  {
    path: "/order-management",
    component: Layout,
    redirect: "/order-management",
    name: "OrderManagement",
    children: [
      {
        path: "/order",
        component: () => import("@/views/order/index.vue"),
        name: "Order",
        meta: { title: "订单管理", icon: "order" },
      },
    ],
  },
  {
    path: "/lawyer-fee-management",
    component: Layout,
    redirect: "/lawyer-fee-management",
    name: "LawyerFeeManagement",
    children: [
      {
        path: "/lawyer-fee",
        component: () => import("@/views/lawyerfee/index.vue"),
        name: "LawyerFee",
        meta: { title: "律师费用", icon: "lawyer-fee" },
      },
    ],
  },
  {
    path: "/session-management",
    component: Layout,
    redirect: "/session-management",
    name: "SessionManagement",
    children: [
      {
        path: "/session",
        component: () => import("@/views/session/index.vue"),
        name: "Session",
        meta: { title: "会话管理", icon: "session" },
      },
    ],
  },
  {
    path: "/advisory-management",
    component: Layout,
    redirect: "/advisory-management",
    name: "AdvisoryManagement",
    meta: { title: "咨询管理", icon: "advisory-management" },
    children: [
      {
        path: "/reservation-info",
        component: () => import("@/views/reservationinfo/index.vue"),
        name: "ReservationInfo",
        meta: { title: "预约信息管理", icon: "reservation-info" },
      },
      {
        path: "/scheduling",
        component: () => import("@/views/scheduling/index.vue"),
        name: "Scheduling",
        meta: { title: "律师排班", icon: "scheduling" },
      },
      {
        path: "/period",
        component: () => import("@/views/period/index.vue"),
        name: "Period",
        meta: { title: "排班时段", icon: "period" },
      },
    ],
  },
  // 外部链接
  // {
  //   path: "/external-link",
  //   component: Layout,
  //   children: [ {
  //       component: () => import("@/views/external-link/index.vue"),
  //       path: "https://www.cnblogs.com/haoxianrui/",
  //       meta: { title: "外部链接", icon: "link" },
  //     },
  //   ],
  // },
  // 多级嵌套路由
  /* {
         path: '/nested',
         component: Layout,
         redirect: '/nested/level1/level2',
         name: 'Nested',
         meta: {title: '多级菜单', icon: 'nested'},
         children: [
             {
                 path: 'level1',
                 component: () => import('@/views/nested/level1/index.vue'),
                 name: 'Level1',
                 meta: {title: '菜单一级'},
                 redirect: '/nested/level1/level2',
                 children: [
                     {
                         path: 'level2',
                         component: () => import('@/views/nested/level1/level2/index.vue'),
                         name: 'Level2',
                         meta: {title: '菜单二级'},
                         redirect: '/nested/level1/level2/level3',
                         children: [
                             {
                                 path: 'level3-1',
                                 component: () => import('@/views/nested/level1/level2/level3/index1.vue'),
                                 name: 'Level3-1',
                                 meta: {title: '菜单三级-1'}
                             },
                             {
                                 path: 'level3-2',
                                 component: () => import('@/views/nested/level1/level2/level3/index2.vue'),
                                 name: 'Level3-2',
                                 meta: {title: '菜单三级-2'}
                             }
                         ]
                     }
                 ]
             },
         ]
     }*/
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
